#include "SudokuLockedCandidatesPointing.h"

SudokuLockedCandidatesPointing::SudokuLockedCandidatesPointing(QObject* parent) :
        SudokuAbstractSolvingTechnique("LC Pointing",
                                       LockedCandidatesPointing, EliminateCandidate, parent)
{
}

int SudokuLockedCandidatesPointing::solve()
{
    //TODO test that method!!!
    qDebug() << ">>SudokuLockedCandidatesPointing::solve";

    for(int number=0; number<mSudokuData->getSize(); ++number)
    {
        for(int boxInd=0;boxInd<mSudokuData->getSize();++boxInd)
        {
            QBitArray colCount(mSudokuData->getSizeBox());
            QBitArray rowCount(mSudokuData->getSizeBox());
            const SudokuHouse* box = mSudokuData->houseAtBox(boxInd);//&boxes[boxInd];
            //qDebug() << "box->count()" << box->count();
            bool boxContainsNumber = false;
            for(int cellInd=0; cellInd<mSudokuData->getSize(); ++cellInd)
            {
                //if box contains number then no need to check that box
                //because there is no cell with possibl == true
                SudokuCell* cell = box->at(cellInd);//TODO change to []
                if(!cell->isEmpty())
                {
                    if(cell->getValue() == number+1)
                    {
                        qDebug() << "SudokuData::findLockedCandidatesPointing break";
                        boxContainsNumber = true;
                        cellInd = mSudokuData->getSize();//just to break loop
                    }
                }
                else if(cell->getPossibl(number))
                {
                    //Use unknown as it is not usual use and could set again
                    colCount.setBit(cell->getColInBox(), true);
                    rowCount.setBit(cell->getRowInBox(), true);
                }
            }
            if(!boxContainsNumber && colCount.count(true) == 1)
            {
                SudokuHouse* house = mSudokuData->houseAtCol((boxInd%mSudokuData->getSizeBox()) * mSudokuData->getSizeBox() + getFirstTrueIndex(colCount));
                mCount += house->eliminate(number,LockedCandidatesPointing,-1,-1,boxInd);
            }
            if(!boxContainsNumber && rowCount.count(true) == 1)
            {
                SudokuHouse* house = mSudokuData->houseAtRow((boxInd/mSudokuData->getSizeBox())*mSudokuData->getSizeBox() + getFirstTrueIndex(rowCount));
                mCount += house->eliminate(number,LockedCandidatesPointing,-1,-1,boxInd);
            }

        }
    }
    qDebug() << "<<SudokuLockedCandidatesPointing::solve " << mCount;
    return mCount;
}

